package edu.geo.filters;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vividsolutions.jts.geom.Coordinate;

import edu.geo.containers.GeoJson;
import edu.geo.containers.Json;
import edu.geo.containers.geom.impl.LineSegment;
import edu.geo.containers.geom.impl.Point;
import edu.geo.containers.geomdomain.GeometricDomain;
import edu.geo.exception.GeoInfoException;
import edu.geo.protocol.Properties;
import edu.geo.protocol.impl.GeomDomainDeleteResponse;
import edu.geo.service.IGeometricDomainServiceFacade;


/*
 * Ejemplo de petición e borrado de un dominio geométrico
 
{
    "type": "operation",
    "name": "deleteFeature",
    "domainId": 30,
    "features": [
        {
            "type": "Feature",
            "properties": {
                "result": "LINESTRING (0 0,  0 -5)",
                "id": "99"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        0,
                        0
                    ],
                    [
                        0,
                        -5
                    ]
                ]
            },
            "crs": {
                "type": "name",
                "properties": {
                    "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
                }
            }
        }
    ]
}
 
 */

public class DeleteFeature extends GeoProcessDecorator
{
	
	private static final Log LOGGER = LogFactory
			.getLog(GeometricDomainAddProcessor.class);
	
	public static final String OPERATION_NAME = "deleteGeometricDomainResult";
	public static final String OPERATION_RESULT_SUCCESSFULLY = "successfully";
	public static final String OPERATION_RESULT_DAMAGED = "damaged";

	private IGeometricDomainServiceFacade geomDomainService;
	
	public DeleteFeature(IGeometricDomainServiceFacade geomDomainService) {
		super();
		this.geomDomainService = geomDomainService;
	}

	@Override
	public Json dispatchResult(Json requestParams) {
		Json result = null;
		Json jsonFeatures = null;
		try {
			
		    int domainId =requestParams.getDomainId();
			Json feature = requestParams.getFeatures().get(0);
		    String  featId =feature.getProperties().getId();
		    int id = Integer.parseInt(featId);
		    this.geomDomainService.deleteFeature(domainId,id);
		    GeometricDomain features = this.geomDomainService.retrieve(domainId);
			List<Point> points = features.getPoints();
			jsonFeatures= new Json(); 	
			jsonFeatures.setName("resultGeometry");
			jsonFeatures.setType("FeatureCollection");
		    ArrayList<Json> featureList= new ArrayList<Json>();
			
			for(Point point:points)
			  {
				 Json jfeature = new Json();
				 jfeature.setType("Feature");
				 GeoJson geoJson= new GeoJson();
				 geoJson.setType("Point");
				 ArrayList<Double> coordinates = new ArrayList<Double>();
				 coordinates.add(point.getX());
				 coordinates.add(point.getY());
				 geoJson.setCoordinates(coordinates);
				 jfeature.setGeometry(geoJson);
				 Properties prp = new Properties();
				 prp.setId(point.getId()+"");
				 prp.setResult(""+point.getAsWKT());
				 jfeature.setProperties(prp);
				 featureList.add(jfeature);				
			  }
			 
			List<LineSegment> Lines = features.getLineSegments();
			
			 for(LineSegment line:Lines)
			  {
				  Json jfeature= new Json();
				  jfeature.setType("Feature");
					GeoJson geoJson= new GeoJson();
					geoJson.setType("LineString");
					ArrayList<ArrayList<Double>> coordinates = new ArrayList<ArrayList<Double>>();
					Coordinate[] coords = line.getCoordinates();
					int numCoords=coords.length;
					for(int i =0;i<numCoords;i++)
					{
						ArrayList<Double> coord= new ArrayList<Double>();
						coord.add(coords[i].x);
						coord.add(coords[i].y);
						coordinates.add(coord);
					}
					geoJson.setCoordinates(coordinates);
					jfeature.setGeometry(geoJson);
					Properties prp = new Properties();
					prp.setId(""+line.getId());
					prp.setResult(""+line.getAsWKT());
					jfeature.setProperties(prp);
					featureList.add(jfeature);			
			  }
			 jsonFeatures.setFeatures(featureList);
		} catch (GeoInfoException e) {
			LOGGER.error(e.getMessage(),e);
		}	
		return jsonFeatures;
	}

}
